package com.dd.dao;

import java.sql.Timestamp;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.DdUserpay;
import com.dd.util.PageList;

public class DdUserpayDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DdUserpayDAO.class);

	public void saveOrUpdate(DdUserpay transientInstance) {
		log.debug("saving DdUserpay instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DdUserpay persistentInstance) {
		log.debug("deleting DdUserpay instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DdUserpay findById(java.lang.Integer id) {
		log.debug("getting DdUserpay instance with id: " + id);
		try {
			DdUserpay instance = (DdUserpay) getHibernateTemplate().get(
					"com.dd.entity.DdUserpay", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public DdUserpay findByOrderid(int orderid, int paytype) {
		log.debug("getting DdUserpay findByOrderid: " + orderid);
		try {
			List li = this.getHibernateTemplate().find(
					"from DdUserpay where orderid=" + orderid + " and paytype="
							+ paytype);
			if (li.size() > 0) {
				return (DdUserpay) li.get(0);
			} else {
				return null;
			}

		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all DdUserpay instances");
		try {
			String queryString = "from DdUserpay";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findDetailByUserid(int userid) {
		log.debug("findDetailByUserid" + userid);
		try {

			return getHibernateTemplate().find(
					"from DdUserpay where shopid=" + userid);
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}

	public List findByShopid(java.lang.Integer shopid) {
		log.debug("findByShopid" + shopid);
		try {

			return getHibernateTemplate().find(
					"from DdUserpay where shopid=" + shopid);
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}

	public List findByShopidAndStatusAndWhere(int shopid, int status,
			String where) {
		log.debug("findByShopidAndStatus" + shopid);
		try {

			return getHibernateTemplate().find(
					"from DdUserpay where shopid=" + shopid + " and status="
							+ status + " " + where);
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}

	public List findByUserid(int userid) {
		log.debug("findByUseridAndStatus" + userid);
		try {
			return getHibernateTemplate().find(
					"from DdUserpay where userid=" + userid
							+ " order by id desc");
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}

	public List findByUseridAndStatusAndWhere(int userid, int status,
			String where) {
		log.debug("findByUseridAndStatus" + userid);
		try {
			return getHibernateTemplate().find(
					"from DdUserpay where userid=" + userid + " and status="
							+ status + " " + where);
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}

	public List findByType(java.lang.Integer type) {
		log.debug("findByType" + type);
		try {
			return getHibernateTemplate().find(
					"from DdUserpay where paytype=" + type);
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}

	public List findByTypeAndStatusAndWhere(int type, int status, String where) {
		log.debug("findByTypeAndStatus" + type + " " + status);
		try {
			return getHibernateTemplate().find(
					"from DdUserpay where paytype=" + type + " and status="
							+ status + " " + where);
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}

	public PageList findByTypeAndStatusAndWhere(String sqlwhere, int pageNo,
			int pageSize) {
		log.debug("findByTypeAndStatusAndWhere");
		try {
			PageList pl = new PageList();
			String queryString = "from DdUserpay a ,DdUser b where 1=1 and a.userid=b.id " + sqlwhere;
			String maxsizeStr = (" select count(*) " + queryString);
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			q = this.getSession().createQuery(
					queryString + " order by a.id desc");
			q.setFirstResult((pageNo - 1) * pageSize);
			q.setMaxResults(pageSize);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);
			return pl;
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}

	}
}